$(function(){
	 // 调用商圈列表
	 selectAreaList();
	 //调用分类列表 
	 selectTypeList();
	 //商圈的切换 
	 var isSelect=null;
	 var column=2;
	 
})
//点击"全部分类"
function show(){
	$('#ceng').show()
}
//点击"取消"按钮
function cancal(){
	$('.twoClassify>li').removeClass("twoType");
	$('#ceng').hide();
}
//点击确定
function sumit(){
	var tijiao1=sessionStorage.getItem('postt').split(',');
	$('.twoClassify>li').removeClass("twoType");
	shangquanShop (tijiao1)
	xinxijiazai(tijiao1)
	 $('#ceng').hide()
 }
// 点击每一个商圈
function shangquanTab(that,shangquanId){
	// console.log(shangquanId)
allShangquan()
	if($('.xianshi>p').css('color','#ff9907')){
		$('.xianshi>p').css('color','#666');
	}
	$('.item').css('color','#666');
	$(that).css('color','#ff9907');
	shangquanShop (shangquanId)
	xinxijiazai(shangquanId)
}
//点击所有商圈
function allShangquan(id){
	 $('.item').css('color','#666');
	 $('.xianshi>p').css('color','#ff9907');
	 var goodType="";
	 var sortTypeIds="";
	 shangquanShop(id);
	 var count=1;
	 if(count==1){
		  $.ajax({
		 url: urlcore + '/api/wantBuy/selectList?current=1&sortTypeId=' + goodType + "&sort=2&sortTypeId=" + goodType ,
		 type: "get",
		 dataType: 'json',
		 contentType: "application/json;charset=utf-8",
		 success: function(json) {
		 // console.log(json)
		 if (json.data && json.success) {
		 	var askToBuyList = json.data.list;
		 	for(let obj of askToBuyList){
		 		var timeStr = timezhuan(obj.uptDatetime)
		 		if (timeStr.split(" ").length > 1){
		 			obj.time2 = timeStr + "发布";
		 			obj.time3 = timeStr.split(" ")[0] + "发布";
		 			obj.time4 = timeStr.split(" ")[0] + "发布";
		 			var mainList='<div class="main_center_list">'+
		 					'<div class="main_center_list_t"><img src="'+obj.coverUrl+'" class="main_img"/>'+
		 					'<img src="images/add/qiugou_right.png" class="main_img1"/></div>'+
		 					'<p>'+obj.time2+'</p>'+
		 				'</div>';
		 			var mainList1='<div class="main_center_list2">'+
		 					'<div class="main_center_list_t1"><img src="'+obj.coverUrl+'" class="main_img"/>'+
		 					'<img src="images/add/qiugou_right.png" class="main_img1"/></div>'+
		 					'<p>'+obj.time3+'</p>'+
		 				'</div>';
		 			var mainList2='<div class="main_center_list3">'+
		 				'<div class="main_center_list_t2"><img src="'+obj.coverUrl+'" class="main_img"/>'+
		 				'<img src="images/add/qiugou_right.png" class="main_img1"/></div>'+
		 					'<p>'+obj.time4+'</p>'+
		 				'</div>';										
		 			$('.margin_list').append(mainList);
		 			$('.margin_list1').append(mainList1);
		 			$('.margin_list2').append(mainList2);
		 			// $('.margin_list3').append(mainList);
		 		}else{
		 			obj.time2 = timeStr;
		 			obj.time3 = timeStr;
		 			obj.time4 = timeStr;
		 		}
		 	}
		 	}
		 }
		 })
	    return count;
	 }else if(count==2){
		 $.ajax({
		 	url: urlcore + '/api/poop/selectPoopList?current=1&tradeAreaId=' + areaId + "&sortTypeId=" + goodType + "&sort=2&sortTypeIds"+classifyId,
		 	type: "get",
		 	dataType: 'json',
		 	contentType: "application/json;charset=utf-8",
		 	success: function(json) {
		 		if (json.data && json.success) {
		 			var askToBuyList = json.data.list;
		 			for(let obj of askToBuyList){
		 						var mainList='<div class="main_center_list">'+
		 														'<div class="main_center_list_c"><img src="'+obj.goods.coverUrl+'" class="main_img"/>'+
		 														'<img src="images/add/weihuo.png" class="main_img1"/></div>'+
		 														'<div class="jiage">'+
		 															'<div class="cuojia">$'+obj.oldPackPrice+'</div>'+
		 															'<div class="zhenjia">$'+obj.goods.packPrice+'</div>'+
		 														'</div>'+
		 														'<p>剩余'+obj.goods.leftNum+'件</p>'+
		 													'</div>';
		 						var mainList1='<div class="main_center_list2">'+
		 														'<div class="main_center_list2_c2"><img src="'+obj.goods.coverUrl+'" class="main_img2"/>'+
		 														'<img src="images/add/weihuo.png" class="main_img3"/></div>'+
		 														'<div class="jiage1">'+
		 															'<div class="cuojia1">$'+obj.oldPackPrice+'</div>'+
		 															'<div class="zhenjia1">$'+obj.goods.packPrice+'</div>'+
		 														'</div>'+
		 														'<p>剩余'+obj.goods.leftNum+'件</p>'+
		 													'</div>';
		 						var mainList2='<div class="main_center_list3">'+
		 														'<div class="main_center_list3_c3"><img src="'+obj.goods.coverUrl+'" class="main_img4"/>'+
		 														'<img src="images/add/weihuo.png" class="main_img5"/></div>'+
		 														'<div class="jiage2">'+
		 															'<div class="cuojia2">$'+obj.oldPackPrice+'</div>'+
		 															'<div class="zhenjia2">$'+obj.goods.packPrice+'</div>'+
		 														'</div>'+
		 														'<p>剩余'+obj.goods.leftNum+'件</p>'+
		 													'</div>';										
		 												$('.margin_list3').append(mainList);
		 												$('.margin_list4').append(mainList1);
		 												$('.margin_list5').append(mainList2);
		 				
		 				var timeStr = timezhuan(obj.uptDatetime)
		 			}
		 			}
		 	}
		 })		
	 }
	 weihuoXinxin(count);

 }
 //点击尾货信息和尾货市场
function weihuoXinxin(te){
	// shangquanShop();
	 // allShangquan();
	$('.lis>p').css({
		"color":"#666",
		"background":"#fff",
	});
	$(te).css({
		"color":"#fff",
		"background":"#ff9907",
		'border-radius':'50px',
	})
	$(".main_center").removeClass("show").addClass('hide');
	$('#' +$(te).attr('name') ).addClass('show').removeClass('hide');	
	shangquanShop();
	// return count;
	
}
//点击图片切换nav的内容
function qiehuanTab(te,column){
   if(column==3){
		 alert('一行三列');
		 $('.position_img1').hide();
		 $('.' +$(te).attr('name') ).show();
		 $(".main_center0").removeClass("show").addClass('hide');
		 $('#' +$(te).attr('name') ).addClass('show').removeClass('hide');
		 $('#' +$(te).attr('data-id') ).addClass('show').removeClass('hide');

		 column=column+1
		 if(column==4){
			 $('.' +$(te).attr('name') ).next().show();
			 $('.' +$(te).attr('name') ).hide(); 
			 
		 }
		
	 }else if(column==4) {
		  alert('一行四列');
		  $('.position_img1').hide();
		  $('.' +$(te).attr('name') ).show();
			$(".main_center0").removeClass("show").addClass('hide');
			$('#' +$(te).attr('name') ).addClass('show').removeClass('hide');
			$('#' +$(te).attr('data-id') ).addClass('show').removeClass('hide');
			
		  column=column-2;
			if(column==2){
				$('.' +$(te).attr('name') ).next().show();
				$('.' +$(te).attr('name') ).hide(); 
			}
	 }else if(column==2){
		 alert('一行两列');
		 $('.position_img1').hide();
		 $('.' +$(te).attr('name') ).show();
		 $(".main_center0").removeClass("show").addClass('hide');
		 $('#' +$(te).attr('name') ).addClass('show').removeClass('hide');
		 $('#' +$(te).attr('data-id') ).addClass('show').removeClass('hide');
		 
		 column=column+1
		 if(column==3){
		 	$('.' +$(te).attr('name') ).hide(); 
			$('.tabb1' ).show();
		 }
	 }
}
//打开二级分类
function oneClassifyShow(that,id){
  var loginShow=false;
	 $('.img2').show();
	 $('.img1').hide();
	 var bele=sessionStorage.getItem('bale').split(',');
			for(var i=0;i<bele.length;i++){
				 if(('twoClasslist'+id)==bele[i]){
					 $('.twoClasslist'+id).toggle();
					 //$(".twoClasslist"+$('.twoClasslist').attr("name")).toggle();
				 }
			}	
 }
//二级分类实现多选
function TwoClassifySelect (that,id,oneId) {
	$('.quanFixrd_bottom>div:first-child').css({
		'color':'#fff',
		'background':'#ff9907'
	});
	if($(that).attr('name')=='trr'){
		$(that).attr("name","br");
		$(that).addClass('twoType');
	}else{
		$(that).attr("name","trr");
		$(that).removeClass('twoType');
	}
	var tijiao=sessionStorage.getItem('ppost').split(',');
	var oneID=$(that).attr('data-id');
	if(oneID==oneId){
		for(var i=0;i<tijiao.length;i++){
			var postt=[];
			postt.push(id)
			sessionStorage.setItem('postt',postt)
		}
	}
}
 //获取商圈列表
function  selectAreaList() {
	$.ajax({
		url: urlcore + '/api/tradeArea/selectList',///tradeArea/selectList
		type: "get",
		dataType: 'json',
		contentType: "application/json;charset=utf-8",
		success: function(json) {
			 var list=json.data;
			 if (json.data && json.success) {
				 var shangquanList='';
				 $.each(json.data,function(i,n){
					  n.areaName = n.areaName.replace("商圈","")
					  shangquanList='<li class="item" name="'+n.id+'" onclick="shangquanTab(this,'+n.id+')">'+
									'<img src="'+n.areaUrl+'"/>'+n.areaName+'</li>';
					  $('.list').append(shangquanList)
						sessionStorage.setItem('id',n.id)
						//首次渲染 
            shangquanShop(n.id)
						xinxijiazai(n.id)
						//切换全部商圈
						allShangquan(n.id)
						// weihuoXinxin(n.id)
				 })
					
			 }else{
				 alert(data.msg)
			 }
		},
		error:function(){
			alert(Error)
		}
	})
}	
// 获取商圈的商品
function shangquanShop (areaId,classifyId) {
	$('.margin_list3').html('');
	$('.margin_list4').html('');
	$('.margin_list5').html('');
	var goodType="";
	var sortTypeIds="";
	$.ajax({
		url: urlcore + '/api/poop/selectPoopList?current=1&tradeAreaId=' + areaId + "&sortTypeId=" + goodType + "&sort=2&sortTypeIds"+classifyId,
		type: "get",
		dataType: 'json',
		contentType: "application/json;charset=utf-8",
		success: function(json) {
			if (json.data && json.success) {
        var askToBuyList = json.data.list;
        for(let obj of askToBuyList){
							var mainList='<div class="main_center_list">'+
															'<div class="main_center_list_c"><img src="'+obj.goods.coverUrl+'" class="main_img"/>'+
															'<img src="images/add/weihuo.png" class="main_img1"/></div>'+
															'<div class="jiage">'+
															  '<div class="cuojia">$'+obj.oldPackPrice+'</div>'+
																'<div class="zhenjia">$'+obj.goods.packPrice+'</div>'+
															 '</div>'+
															'<p>剩余'+obj.goods.leftNum+'件</p>'+
														'</div>';
						  var mainList1='<div class="main_center_list2">'+
															'<div class="main_center_list2_c2"><img src="'+obj.goods.coverUrl+'" class="main_img2"/>'+
															'<img src="images/add/weihuo.png" class="main_img3"/></div>'+
															'<div class="jiage1">'+
																'<div class="cuojia1">$'+obj.oldPackPrice+'</div>'+
																'<div class="zhenjia1">$'+obj.goods.packPrice+'</div>'+
															'</div>'+
															'<p>剩余'+obj.goods.leftNum+'件</p>'+
														'</div>';
						  var mainList2='<div class="main_center_list3">'+
															'<div class="main_center_list3_c3"><img src="'+obj.goods.coverUrl+'" class="main_img4"/>'+
															'<img src="images/add/weihuo.png" class="main_img5"/></div>'+
															'<div class="jiage2">'+
																'<div class="cuojia2">$'+obj.oldPackPrice+'</div>'+
																'<div class="zhenjia2">$'+obj.goods.packPrice+'</div>'+
															'</div>'+
															'<p>剩余'+obj.goods.leftNum+'件</p>'+
														'</div>';										
													$('.margin_list3').append(mainList);
													$('.margin_list4').append(mainList1);
													$('.margin_list5').append(mainList2);
					
          var timeStr = timezhuan(obj.uptDatetime)
        }
		    }
		}
	})		
}
//尾货信息、尾货市场的商品加载
 function xinxijiazai(areaId,classifyId){
		$('.margin_list').html('');
		$('.margin_list1').html('');
		$('.margin_list2').html('');
		var goodType="";
		var sortTypeIds="";
		$.ajax({
			url: urlcore + '/api/tailGoods/selectList?current=1&tradeAreaId=' + areaId + "&sortTypeId=" + goodType + "&sort=2&sortTypeIds"+classifyId,
			type: "get",
			dataType: 'json',
			contentType: "application/json;charset=utf-8",
			success: function(json) {
				if (json.data && json.success) {
	        var askToBuyList = json.data.list;
	        for(let obj of askToBuyList){
	          var timeStr = timezhuan(obj.uptDatetime)
	          if (timeStr.split(" ").length > 1){
	            obj.time2 = timeStr + "发布";
	            obj.time3 = timeStr.split(" ")[0] + "发布";
	            obj.time4 = timeStr.split(" ")[0] + "发布";
							var mainList='<div class="main_center_list">'+
									'<div class="main_center_list_c"><img src="'+obj.coverUrl+'" class="main_img"/>'+
									'<img src="images/add/weihuo.png" class="main_img1"/></div>'+
									'<p>'+obj.time2+'</p>'+
								'</div>';
							var mainList1='<div class="main_center_list2">'+
									'<div class="main_center_list2_c2"><img src="'+obj.coverUrl+'" class="main_img2"/>'+
									'<img src="images/add/weihuo.png" class="main_img3"/></div>'+
									'<p>'+obj.time3+'</p>'+
								'</div>';
							var mainList2='<div class="main_center_list3">'+
									'<div class="main_center_list3_c3"><img src="'+obj.coverUrl+'" class="main_img4"/>'+
									'<img src="images/add/weihuo.png" class="main_img5"/></div>'+
									'<p>'+obj.time4+'</p>'+
								'</div>';										
							$('.margin_list').append(mainList);
							$('.margin_list1').append(mainList1);
							$('.margin_list2').append(mainList2);
	          }else{
	            obj.time2 = timeStr;
	            obj.time3 = timeStr;
	            obj.time4 = timeStr;
	          }
	        }
			    }
			}
		})		
	}
// 获取分类列表 
 function  selectTypeList() {
	$.ajax({
		url: urlcore + '/api/sortType/selectTwoList',
		type: "get",
		dataType: 'json',
		contentType: "application/json;charset=utf-8",
		success: function(json) {
			var list=json.data;
			if (json.data&& json.success) {
				var oneClassifyList='';
			  var selsect=[];
				var ppost=[];
				$.each(json.data.list ,function(i,n){
					 oneClassifyList+='<div  class="li" onclick="oneClassifyShow(this,'+n.id+')">'+
								'<div class="oneclassify">'+n.sortName+'</div>'+
								'<div class="oneclassify_img">'+
									'<img src="images/add/xia.png" class="img1"/>'+
									'<img src="images/add/shang.png"  class="img2"/>'+
								'</div>'+
							'</div><div class="twoClasslist'+n.id+' twoClasslist" name="'+n.id+'"><ul class="twoClassify" data-id="'+n.id+'">';
						$.each(n.sortTypeList,function(i,m){
							 oneClassifyList+='<li><div style="width:100%;height:100%;" name="trr" data-id="'+m.oneTypeId+'"  onclick="TwoClassifySelect(this,'+m.id+','+n.id+')">'+m.sortName+'</div></li>';
						   ppost.push(m.id)
							 sessionStorage.setItem('ppost',ppost)
						})  
						//存储class的类名 
						selsect.push('twoClasslist'+n.id)
						sessionStorage.setItem('bale',selsect)
            oneClassifyList+='</ul></div>';	
				})
				$('.ol').append(oneClassifyList,)	
			}else{
				alert(data.msg)
			}
		},
		error:function(){
			alert(Error)
		}
	})
}
//点击发布 
function qiugoujia(){
	if(!alert('需要您登陆才能继续')){
		window.location.href = "1.0.0denglu.html";
		if(tpken){
				window.location.href = "4.2.7qiugou_fabu.html";
		}
	}

}
//时间转换 
function timezhuan(time) {
    var that = this;
    var nowTime = Date.parse(new Date());
    var subTime = (nowTime - Date.parse(new Date(time))) / 1000;
    var timeStr = '';
    if (subTime < 60) {
      timeStr = '刚刚发布';
    } else if (parseInt(subTime / 60) < 60) {
      timeStr = parseInt((subTime / 60)) + '分钟前发布';
    } else if (parseInt(subTime / (60 * 60)) < 24) {
      timeStr = parseInt(subTime / (60 * 60)) + '小时前发布';
    } else if (parseInt(subTime / (60 * 60 * 24)) < 2) {
      timeStr = '昨天' + time.split(" ")[1] +"发布";
    } else if (parseInt(subTime / (60 * 60 * 24)) < 3) {
      timeStr = '前天' + time.split(" ")[1] + "发布";
    } else {
      timeStr = time;
    }
    return timeStr;
  }
   